'use strict';

import React from 'react';
import { Form, Input, Spin, message, Modal,DatePicker,Switch} from 'antd';
import HttpUtil from '../../lib/utils/HttpUtil';
import moment from 'moment';
import Pubsub from 'pubsub-js';

const FormItem = Form.Item;

class AddUpdateForm extends React.Component{

  constructor(props){
    super(props);
    this.state = {
        loading:false,
        active:false
    }
  }

  componentWillReceiveProps(nextProps) {
       if(!nextProps.visible){
           this.props.form.resetFields();
       }
   }

  handleCommit(e){
    e.preventDefault();
    let self = this;
    this.props.form.validateFields((err, values) => {
      if (!err) {

        /* values 格式修改 如values.birthday = values.birthday?values.birthday.format("YYYY-MM-DD HH:mm:ss")*/


        if(values.${pkName}){
          HttpUtil.put("/${lowerClassName}/",values).then((response)=>{
            Pubsub.publish('search');
            self.props.onClose();
          },(result)=>{
            message.error(result.message);
          })
        }else{
          let self = this;
          HttpUtil.post("/${lowerClassName}/",values).then((response)=>{
            Pubsub.publish('search');
            self.props.onClose();
          },(result)=>{
            message.error(result.message);
          })
        }
      }
    });


  }

  render(){
    const onClose = this.props.onClose;
    const visible = this.props.visible;

    const {
    /*
     * 删除无用属性 TODO
     */
    <#list columnInfoList as columnInfo>
        <#if columnInfo_has_next>
        ${columnInfo.lowerCamelName},
        <#else>
        ${columnInfo.lowerCamelName}
        </#if>
    </#list>
    } = this.props.editItem;
    const { getFieldDecorator } = this.props.form;
    const formItemLayout = {
      labelCol: { span: 6 },
      wrapperCol: { span: 14 },
    };
    return(
      <Modal
        title={${pkName}?"修改":"新增"}
        disabled
        onOk={ this.handleCommit.bind(this) }
        onCancel={ onClose }
        okText="确定"
        cancelText="取消"
        visible={visible}
        width="520px">

          <Form onSubmit={this.handleSubmit} className="login-form">

          {/*
           * 删除无用列并修改输入框（校验类型） TODO
           */}
          <#list columnInfoList as columnInfo>
          <#if columnInfo.remark??>
            <FormItem {...formItemLayout} label="${columnInfo.remark}">
              {getFieldDecorator('${columnInfo.lowerCamelName}',{
                initialValue: ${columnInfo.lowerCamelName},
                rules: [{ required: true, message: '请输入${columnInfo.remark}' }]
              })(
              <#if columnInfo.lowerCamelName == pkName>
                <Input type="hidden"/>
              <#else>
                <Input/>
              </#if>
              )}
            </FormItem>
          </#if>
          </#list>
          </Form>
      </Modal>
    );
  }
}

const ${className}Form = Form.create()(AddUpdateForm);

export default ${className}Form
